//
//  CRTopic.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/27.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRTopic.h"
#import "CRComment.h"
#import "CRUser_MK.h"
#import "MJExtension.h"
@implementation CRTopic

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{
             @"ID" : @"id",
             @"small_image" : @"image0",
             @"middle_image" : @"image2",
             @"large_image" : @"image1",
             @"top_cmt" : @"top_cmt[0]"
             };
}

// 计算cell的自适应高度
- (CGFloat)cellHeight {
    
    if (!_cellHeight) {
        // 顶部头像高度
        _cellHeight = CRTopicProfileImageMaxY + CRMargin;
        
        // 文字高度
        CGSize maxTextSize = CGSizeMake(CRScreenW - 2 * CRMargin, MAXFLOAT); // 高为无限制
        _cellHeight += [self.text boundingRectWithSize:maxTextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : CRTopicTextFont} context:nil].size.height + CRMargin;
        
        // 中间内容
        if (self.type != CRTopicTypeWord) { // 中间有内容 视频 音频 图片
            CGFloat contentX = CRMargin;
            CGFloat contentY = _cellHeight;
            CGFloat contentW = maxTextSize.width;
            CGFloat contentH = contentW * self.height / self.width; // 按比例缩放
            // 如果图片帖子的内容高度过高，那么就限制一下
            if (self.type == CRTopicTypePicture && contentH > CRScreenH - 108) {
                contentH = 300;
                
                // 大图片
                self.bigPicture = YES;
            }
            self.contentFrame = CGRectMake(contentX, contentY, contentW, contentH);
            _cellHeight += contentH + CRMargin;
        }
        // 最热评论
        if (self.top_cmt) {
            NSString *commentText = [NSString stringWithFormat:@"%@ : %@", self.top_cmt.user.username, self.top_cmt.content];
            _cellHeight += CRTopicTopCmtTitleH + [commentText boundingRectWithSize:maxTextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : CRTopicTextFont} context:nil].size.height + CRMargin;
        }
        
        // 工具条
        _cellHeight += CRTopicToolbarH + CRMargin;
    }
    return _cellHeight;
}

- (NSString *)created_at {
    // 创建1次formatter
    static NSDateFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
    });
    
    // 获得日期对象
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createDate = [formatter dateFromString:_created_at];
    // 获得createDate和当前时间的差值
    NSDateComponents *cmps = createDate.intervalToNow;
    
    // 根据日期对象返回对应的时间字符串
    if (createDate.isThisYear) {
        if (createDate.isToday) { // 今天
            if (cmps.hour >= 1) { // interval >= 1小时  xx小时前
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1小时 > interval >= 1分钟  xx分钟前
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { // 1分钟 > interval  刚刚
                return @"刚刚";
            }
        } else if (createDate.isYesterday) { // 昨天 HH:mm:ss
            formatter.dateFormat = @"昨天 HH:mm:ss";
            return [formatter stringFromDate:createDate];
        } else { // MM-dd HH:mm:ss
            formatter.dateFormat = @"MM-dd HH:mm:ss";
            return [formatter stringFromDate:createDate];
        }
    } else { // yyyy-MM-dd HH:mm:ss
        return _created_at;
    }
    
}

@end
